package com.hi5.stexviewer.gui;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JLabel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hi5.stexviewer.message.Message;
import com.hi5.stexviewer.message.MessageType;
import com.hi5.stexviewer.message.SignalMessage;
import com.hi5.stexviewer.message.impl.JFrameCustomer;

public class TickerFrame extends JFrameCustomer {
    private static final long serialVersionUID = -1905807482144259445L;
    private static final Logger LOGGER = LoggerFactory.getLogger(TickerFrame.class);
    private static final Set<Object> HANDLED_MESSAGE_TYPES;
    
    private JLabel label;
    
    static {
        Set<Object> handledMessageTypes = new HashSet<Object>();
        handledMessageTypes.add(MessageType.RATES_ARRIVED);
        HANDLED_MESSAGE_TYPES = Collections.unmodifiableSet(handledMessageTypes);
    }

    public TickerFrame() {
        label = new JLabel("Hello World!");
        getContentPane().add(label);
        setSize(200, 50);
        setTitle("Stock Exchange Watcher");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        addWindowListener(new MyWindowListener());
    }

    @Override
    protected void doRecieve(Message<?> message) {
        if (MessageType.RATES_ARRIVED == message.getType()) {
            label.setText(String.format("I= %d", message.getData()));
        }
    }

    @Override
    protected Set<Object> getHandledMessageTypes() {
        return HANDLED_MESSAGE_TYPES;
    }

    /**
     * Windows listener. Inner class to encapsulate.
     * @author mate
     *
     */
    private class MyWindowListener implements WindowListener {

        public void windowActivated(WindowEvent e) {
        }

        public void windowClosed(WindowEvent e) {
        }

        public void windowClosing(WindowEvent e) {
            getCallCenter().send(new SignalMessage(MessageType.APPLICATION_STOP));
        }

        public void windowDeactivated(WindowEvent e) {
        }

        public void windowDeiconified(WindowEvent e) {
        }

        public void windowIconified(WindowEvent e) {
        }

        public void windowOpened(WindowEvent e) {
        }
        
    }
}
